package com.elrichuang.userserver.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.elrichuang.userserver.domain.Candidate;
import com.elrichuang.userserver.domain.bo.CandidateQueryBo;
import com.elrichuang.userserver.domain.vo.CandidateStatusRowVo;
import com.elrichuang.userserver.domain.vo.CandidateStatusVo;
import com.elrichuang.userserver.enums.CandidateStatusEnum;
import com.elrichuang.userserver.mapper.CandidateMapper;
import com.elrichuang.userserver.service.ICandidateService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service
public class CandidateServiceImpl extends ServiceImpl<CandidateMapper, Candidate> implements ICandidateService {

	/**
	 * 按状态分组统计
	 * @param bo 筛选条件
	 * @return 分组统计对象
	 */
	@Override
	public CandidateStatusVo allStatus(CandidateQueryBo bo) {
		List<CandidateStatusRowVo> result = baseMapper.selectAllStatus(bo);

		ArrayList<CandidateStatusRowVo> rows = new ArrayList<>();
		//循环所有枚举，构建输出结果
		for (CandidateStatusEnum status : CandidateStatusEnum.values()) {
			// 跳过状态为未知的
			if (status == CandidateStatusEnum.CANDIDATE_STATUS_NONE) {
				continue;
			}

			CandidateStatusRowVo rowVo = new CandidateStatusRowVo();
			rowVo.setStatus(status.getCode());
			rowVo.setStatusStr(status.getInfo());
			// 获取分组统计返回的结果
			for (CandidateStatusRowVo statusRowVo: result) {
				if (statusRowVo.getStatus().equals(status.getCode())) {
					rowVo.setTotal(statusRowVo.getTotal());
					break;
				}else {
					rowVo.setTotal(0L);
				}
			}
			rows.add(rowVo);
		}

		CandidateStatusVo statusVo = new CandidateStatusVo();
		statusVo.setRows(rows);

		return statusVo;
	}
}
